package com.wzy.new_energy_car_platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wzy.new_energy_car_platform.common.GlobalVariable;
import com.wzy.new_energy_car_platform.common.ResultDto;
import com.wzy.new_energy_car_platform.dao.UserCarMapper;
import com.wzy.new_energy_car_platform.model.UserCar;
import com.wzy.new_energy_car_platform.service.UserCarService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 用户车辆表 服务实现类
 * </p>
 *
 * @author wzy
 * @since 2024-04-03
 */
@Service
public class UserCarServiceImpl extends ServiceImpl<UserCarMapper, UserCar> implements UserCarService {
    @Autowired
    private  UserCarMapper userCarMapper;

    @Override
    public ResultDto<?> getUserCar(int userId) {
        LambdaQueryWrapper<UserCar> userCarLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userCarLambdaQueryWrapper.eq(UserCar::getUserId,userId)
                .eq(UserCar::getValidFlag, GlobalVariable.VALID_TRUE);
        UserCar userCar = userCarMapper.selectOne(userCarLambdaQueryWrapper);
        return ResultDto.ok(userCar);
    }

    @Override
    public ResultDto<?> uploadMyCar(UserCar userCar) {
        LambdaQueryWrapper<UserCar> userCarLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userCarLambdaQueryWrapper.eq(UserCar::getUserId,userCar.getUserId())
                .eq(UserCar::getValidFlag,GlobalVariable.VALID_TRUE);
        UserCar car = userCarMapper.selectOne(userCarLambdaQueryWrapper);
        if (Objects.nonNull(car)){
            car.setImg(userCar.getImg());
            userCarMapper.updateById(car);
        }else {
            userCarMapper.insert(userCar);
        }

        return ResultDto.ok();
    }

    @Override
    public ResultDto<?> saveCarInfo(UserCar userCar) {
        LambdaUpdateWrapper<UserCar> userCarLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        userCarLambdaUpdateWrapper.eq(UserCar::getUserId,userCar.getUserId())
                .eq(UserCar::getValidFlag,GlobalVariable.VALID_TRUE);
        UserCar car = userCarMapper.selectOne(userCarLambdaUpdateWrapper);
        if (Objects.nonNull(car)){
            userCar.setId(car.getId());
            userCarMapper.updateById(userCar);
        }else {
            userCarMapper.insert(userCar);
        }
        return ResultDto.ok();
    }
}
